package com.itheima.tanhua.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.ResultInfo;
import com.itheima.domain.db.Admin;
import com.itheima.tanhua.mapper.AdminMapper;
import com.itheima.tanhua.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zsk
 * @create 2022-03-30-12:49
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ResultInfo login(String username, String password) {

        //构建查询条件
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,username);

        //查询用户信息
//        Admin admin = adminMapper.findByUserName(username);

        //查询
        Admin admin = adminMapper.selectOne(wrapper);
        if (admin.getId() == 0){
            return ResultInfo.error("当前用户不存在");
        }
        //查询密码
        String passwordWithMd5 = SecureUtil.md5(password);//对原密码进行md5加密
        if (!StringUtils.equals(passwordWithMd5, admin.getPassword())) {//StringUtils.equals用于对比两个字符串是否相等
            return ResultInfo.error("密码错误");
        }

        //登录成功
        return ResultInfo.success(admin);
    }
}
